﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace asMeishi.web.Models
{
    public class history : MongoDocument
    {
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        [BsonElement("D")]
        public DateTime dateTime { get; set; }
        [BsonElement("U")]
        public ObjectId userId { get; set; }

        [BsonElement("N")]
        public string userName { get; set; }
        /// <summary>
        /// 操作用户的ip
        /// </summary>
        [BsonElement("P")]
        public string ip {get;set; }
        //操作用户的坐标
        [BsonElement("L")]
        public double[] lnglat {get; set;}
        /// <summary>
        /// 操作类型
        /// </summary>
        [BsonElement("T")]
        public string type { set; get; }
        [BsonElement("C")]
        public string targetClass { get; set; }
        /// <summary>
        /// 目标
        /// </summary>
        [BsonElement("I")]
        public ObjectId targetId { get; set; }
        /// <summary>
        /// 目标字段
        /// </summary>
        [BsonElement("F")]
        public string targetField { get; set; }
        [BsonElement("V")]
        public string value { get; set; }

    }

    public class historyC<TEntity>  where TEntity : MongoDocument
    {

        ObjectId targetId;

        DataSession<MongoDataContext, history> db = new DataSession<MongoDataContext, history>();

        public historyC(TEntity item)
        {
            this.targetId = item.Id;
        }
        public historyC(ObjectId Id)
        {
            this.targetId = Id;
        }
        public historyC()
        {

        }
        private history createDefault(user user)
        {
            var history = new history();
            history.userId = user.Id;
            history.userName = user.name;
            history.targetClass = typeof(TEntity).Name;
            history.targetId = this.targetId;
            return history;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id">目标ID</param>
        /// <param name="Field">目标字段</param>
        /// <param name="value">目标值</param>
        /// <param name="user">操作用户</param>
        public static void Add(ObjectId Id,string Field,historyTypeEnum type,string value,user user){
            historyC<TEntity> hc = new historyC<TEntity>(Id);
            hc.add(Field,type, value, user);
        }

        public void add(string Field,historyTypeEnum type, string value,user user)
        {
            var history = createDefault(user);
            history.targetField = Field;
            history.value = value;
            history.type = type.ToString();
            history.dateTime = DateTime.Now;
            db.Save(history);
        }
        public void add(ObjectId targetId, string Field, historyTypeEnum type, string value, user user)
        {
            var history = createDefault(user);
            history.targetField = Field;
            history.value = value;
            history.type = type.ToString();
            history.dateTime = DateTime.Now;
            db.Save(history);
        }
        public IQueryable<history> get()
        {
            if (this.targetId == new ObjectId())
            {
                return db.Where(d => d.targetClass == typeof(TEntity).Name).OrderByDescending(s => s.dateTime);
            }
            else {
                return db.Where(d => d.targetClass == typeof(TEntity).Name && d.targetId== targetId).OrderByDescending(s => s.dateTime);
            }
        }


    }
    public enum historyTypeEnum{
        添加,
        修改,
        删除,
        评价,
        建立,
        修改状态
    }
    public enum historyEnum
    {
        shop,
        food,
        user,
        comment,
        impression
    }
}